﻿using MediatR;
using Microsoft.Extensions.Logging;
using Ordering.Application.Commands;
using Ordering.Domain.Aggregate;
using Ordering.Domain.Events;

namespace Ordering.Application.DomainEvents
{
    internal class UpdateBuyerBalacePaymentOrderDomainEventHandler
        : INotificationHandler<PaymentOrderDomainEvent>
    {
        private readonly ILogger _logger;
        private readonly IMediator _mediator;
        private readonly IBuyerStogage _buyerStogage;

        public UpdateBuyerBalacePaymentOrderDomainEventHandler(
            IMediator mediator,
            IBuyerStogage buyerStogage,
            ILogger<SendEmailOrderDomainEventHandler> logger)
        {
            _logger = logger;
            _mediator = mediator;
            _buyerStogage = buyerStogage;
        }

        public async Task Handle(PaymentOrderDomainEvent notification, CancellationToken cancellationToken)
        {
            var buyer = await _buyerStogage.GetByIdAsync(notification.Target.BuyerId);
            var amount = notification.Target.TotalAmount * -1;
            await _mediator.Send(new UpdateBalanceBuyerCommand(buyer.Id, amount));
        }
    }
}
